#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File   : tsSocketServerServ.py
# @Email  : xuexia_xu@feidee.com
# @Time   : 2024/4/7 13:56
# @Desc   : 《Python核心编程第3版》2.5.1 SocketServer 时间戳 TCP 服务器

from socketserver import (TCPServer as TCP, StreamRequestHandler as SRH)
from time import ctime

HOST = ''
PORT = 21567
ADDR = (HOST, PORT)


class MyRequestHandler(SRH):
    def handle(self):
        print('...connected from:', self.client_address)
        # StreamRequestHandler类将输入和输出套接字看作类似文件的对象，因此我们将使用 readline()来获取客户端消息，并利用 write()将字符串发送回客户端。
        self.wfile.write(b'[%s] %s' % (bytes(ctime(), 'utf-8'), self.rfile.readline()))


tcpServ = TCP(ADDR, MyRequestHandler)
print('waiting for connection...')
tcpServ.serve_forever()
